package reedSolomon;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import fileDistributionStatistics.FileSizeDistributionCalculation;
import fileDistributionStatistics.FileSizeInfo;

public class OriginalFileSizeOnCloud {
	File fFile;
	File filename = new File("/home/bhavya/Desktop/BHAVYA_FYP/Cloud_Computing_FYP/FileFragmentSizeCalculation_2/original.txt");
	int startingFileSize = 1;
	int sumOfSmallFileSizes = 0;
	List<FileSizeInfo> fileInfo = new ArrayList<FileSizeInfo>();
	FileWriter fstream;
	BufferedWriter out;

	public OriginalFileSizeOnCloud(File aFileName) {
		fFile = aFileName;
	}

	public final void processLineByLine() throws FileNotFoundException {
		// Note that FileReader is used, not File, since File is not Closeable
		Scanner scanner = new Scanner(new FileReader(fFile));
		try {

			// first use a Scanner to get each line
			while (scanner.hasNextLine()) {
				processLine(scanner.nextLine());
			}
		} finally {
			scanner.close();
		}
	}

	protected void processLine(String aLine) {
		// use a second Scanner to parse the content of each line
		Scanner scanner = new Scanner(aLine);

		int rep1;
		scanner.useDelimiter(":");
		if (scanner.hasNext()) {
			String fs = scanner.next().trim();
			String rep = scanner.next().trim();
			rep1 = Integer.parseInt(rep);
			// rep1=1;
//			System.out.println(" " + fs + " " + rep1);
			fileInfo.add(new FileSizeInfo(Double.parseDouble(fs), Integer
					.parseInt(rep)));
		}
		scanner.close();
	}

	public void preProcessFileSize() {

		for (int i = 0; i < fileInfo.size(); i++) {
			double fs = fileInfo.get(i).getFileSize();
			if (fs <= startingFileSize) {
				sumOfSmallFileSizes += (fs * fileInfo.get(i).getNumberOfFiles());
			}
		}
		int numberOf40KB = (int) Math.ceil((double) sumOfSmallFileSizes
				/ startingFileSize);
		int index = 0;
		for (int i = 0; i < fileInfo.size(); i++) {
			if (fileInfo.get(i).getFileSize() == startingFileSize) {
				index = i;
				break;
			}
		}
		System.out.println(" is starting source frag present" + index + " "
				+ sumOfSmallFileSizes + " " + numberOf40KB);
		if (index != 0) {
			int existingNumberOf40KB = fileInfo.get(index).getNumberOfFiles();
			System.out.println(" existing num " + existingNumberOf40KB);
			// FileSizeInfo temp = new FileSizeInfo((double)startingFileSize,
			// existingNumberOf40KB);
			// fileInfo.remove(index);
			fileInfo.get(index).setNumberOfFiles(
					existingNumberOf40KB + numberOf40KB);
			// fileInfo.add(new FileSizeInfo((double) startingFileSize,
			// existingNumberOf40KB+numberOf40KB));
		} else {
			fileInfo.add(new FileSizeInfo((double) startingFileSize,
					numberOf40KB));
		}

		System.out.println("done!!");
		int i = 0;
		while (fileInfo.size() != 0) {
			double fs = fileInfo.get(i).getFileSize();
			if (fs < startingFileSize) {
				if (fs == startingFileSize)
					System.out.println(fs);
				fileInfo.remove(i);
				i = 0;
			} else {
				i++;
			}

			if (i == fileInfo.size()) {
				break;
			}
		}
		System.out.println("done removing!!");
		for (i = 0; i < fileInfo.size(); i++) {
			if (fileInfo.get(i).getFileSize() == startingFileSize) {
				System.out.println(startingFileSize+"kb source file"
						+ fileInfo.get(i).getFileSize() + "no of copies"
						+ fileInfo.get(i).getNumberOfFiles());
			}
		}

	}

	public void calculateFragmentSize() {
		// Change the code to read values from the arrayList !!
		// TODO read values from ArrayList Instead of directly reading
		FileSizeDistributionCalculation f = new FileSizeDistributionCalculation();
		try {
			fstream = new FileWriter(filename);
			out = new BufferedWriter(fstream);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        double spaceOccupiedOnCloud=0;
        double cumSpace=0;
		for (int i = 0; i < fileInfo.size(); i++) {
			double source_file = fileInfo.get(i).getFileSize();
			int rep = fileInfo.get(i).getNumberOfFiles();
			try{
				out.write(source_file+",");
			}catch(Exception e){
				e.printStackTrace();
			}
//			double sourceFile=source_file;
			int factor=(int)(source_file%4096);
			if(factor!=0){
				source_file=source_file+(4096-factor);
			}
			System.out.println(source_file%4096);
			spaceOccupiedOnCloud=(source_file)*rep;
//			cumSpace+=spaceOccupiedOnCloud;
			try {
//				System.out.println(source_file+":"+spaceOccupiedOnCloud);
//			out.write(source_file+",");
			out.write(String.valueOf(spaceOccupiedOnCloud)+",");
			out.write(String.valueOf(rep));
			out.newLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        
		}
//		System.out.println(cumSpace);
		try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		File parseFilename = new File("/home/bhavya/Desktop/BHAVYA_FYP/Cloud_Computing_FYP/FileSizeDistributionStatistics/CalculatedfileSizeGreatertan1byte.txt");

		OriginalFileSizeOnCloud parser = new OriginalFileSizeOnCloud(parseFilename);
		try {
			parser.processLineByLine();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		parser.preProcessFileSize();
		parser.calculateFragmentSize();
		// System.out.println("done");
	}
}

